<?php
require_once(ROOT_ADMIN_MODELS.DS.'Models_Base.php'); 
require_once(ROOT_ADMIN_MODELS.DS.'Models_Log.php'); 
require_once(ROOT_ADMIN_MODELS.DS.'Models_Forum.php');
require_once(ROOT_LIBRARY_UTILITY.DS.'utility.php');
require_once(ROOT_ADMIN_CONTROLLERS.DS.'BaseController.php');
require_once(ROOT_ADMIN_FORMS.DS.'Forms_Forum.php');
require_once(ROOT_ADMIN_OBJECT.DS.'Objs_Forum.php');

class Admin_ForumController extends BaseController {	
		public function preDispatch()
		{
			parent::preDispatch();
			if(!$this->hasPrivilege())
				$this->_redirect ("/admin/error/permission");
		}
		
	
	public function indexAction()
	{
		try
		{
			$this->view ->headTitle("UIT-News Supporter");			
			$md = new Models_Forum();
			$arrforum = $md->listforum("");
			$this->view->arrforum = $arrforum;
			$arrforumshort = $md->listforum("sort");
			$this->view->arrforumsort = $arrforumshort;
		}
		catch (Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function statusAction()
	{
		try{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			$md = new Models_Forum();
			$id = $_POST['id'];
			$status = $_POST['status'];
			$md->setstatus($id, $status);
			$arrforumshort = $md->listforum("sort");
			echo(json_encode($arrforumshort));
			Models_Log::insert($this->view->user->id, "act_Update_Status_Forum","id=".$id);
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function deleteAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNorender();
		try{
			$md = new Models_Forum();
			$id = $_POST['id'];
			$count = $md->counttypenew($id);
			if($count == 0)
				$md->delforum($id);
			else
				$md->setstatus($id,-1);
			echo "1";
			Models_Log::insert($this->view->user->id, "act_Delete_Forum","id=".$id);
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function newAction()
	{
		$this->view ->headTitle("UIT-News Supporter");		
		try{
			if($this->_request->isPost())
			{
				$form = new Forms_Forum();
				$form->_requestToForm($this);
				$md = new Models_Forum();
				$form->obj->id = NULL;
				$id = $md->_insert($form->obj);
				Models_Log::insert($this->view->user->id, "act_New_Forum","id=".$id);
				$this->_redirect("/admin/forum/edit/id/".$id);
			}
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function editAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try{
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Typenews.php');
			$md = new Models_Forum();
			$id = $this->_request->getParam('id');
			$forum = $md->getforumbyid($id);
			$this->view->forum = $forum;
			$mdtypenews = new Models_Typenews();
			$arrtypenews = $mdtypenews->listtypenews($id, "");
			$this->view->arrtypenews = $arrtypenews;
			
			$arrtypenewssort = $mdtypenews->listtypenews($id, "sort");
			$this->view->arrtypenewssort = $arrtypenewssort; 
			
			if($this->_request->isPost())
			{
				$form = new Forms_Forum();
				$form->_requestToForm($this);
				$md->_update($form->obj);
				$forum = $md->getforumbyid($id);
				$this->view->forum = $forum;
				Models_Log::insert($this->view->user->id, "act_update_Forum","id=".$form->obj->id);
			}
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function sortAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNorender();
		try{
			$md = new Models_Forum();
			foreach ($_POST['id'] as $key=>$row)
			{
				$md->updatenumber($row, $key+1);
			}
			Models_Log::insert($this->view->user->id, "act_Sort_Forum");
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
}
	